﻿using Hidistro.Core;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_PriceFilter : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.html());
        }
        private string html()
        {
            if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
            {
                StringBuilder sb = new StringBuilder();
                int i;
                int.TryParse(this.Page.Request.QueryString["filter"], out i);
                sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", this.CreateUrl("filter", "0"), i == 0 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>100元及以下</a>", this.CreateUrl("filter", "1"), i == 1 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>101-200元</a>", this.CreateUrl("filter", "2"), i == 2 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>201-300元</a>", this.CreateUrl("filter", "3"), i == 3 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>301-500元</a>", this.CreateUrl("filter", "4"), i == 4 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>500元以上</a>", this.CreateUrl("filter", "5"), i == 5 ? " class=\"kkokk\"" : "");
                return sb.ToString();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                int i;
                int.TryParse(this.Page.Request.QueryString["filter"], out i);
                sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", this.CreateStaticizeUrl(0), i == 0 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>100元及以下</a>", this.CreateStaticizeUrl(1), i == 1 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>101-200元</a>", this.CreateStaticizeUrl(2), i == 2 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>201-300元</a>", this.CreateStaticizeUrl(3), i == 3 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>301-500元</a>", this.CreateStaticizeUrl(4), i == 4 ? " class=\"kkokk\"" : "");
                sb.AppendFormat("<a href=\"{0}\"{1}>500元以上</a>", this.CreateStaticizeUrl(5), i == 5 ? " class=\"kkokk\"" : "");
                return sb.ToString();
            }
        }
        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }


        private string CreateStaticizeUrl(int pos)
        {
            if (this.Page.Request.Path == "/SubCategory.aspx")
            {
                int feature;
                int brand;
                int filter;
                int rank;

                int.TryParse(this.Page.Request.QueryString["feature"], out feature);
                int.TryParse(this.Page.Request.QueryString["brand"], out brand);
                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);

                int categoryId;

                int.TryParse(this.Page.Request.QueryString["categoryId"], out categoryId);


                CategoryInfo cateif = CategoryBrowser.GetCategory(categoryId);

                string rawUrl = this.Context.Request.RawUrl;
                if (rawUrl.ToLower().Contains("/brand"))
                {
                    if (cateif == null)
                        return Globals.GetSiteUrls().StaticizeBrand(0, filter, brand, rank, 0);
                    else
                        return Globals.GetSiteUrls().StaticizeBrand(cateif.CategoryId, filter, brand, rank, 0);
                }
                else
                {
                    if (cateif == null)
                        return Globals.GetSiteUrls().StaticizeSubCategory(0, "", 0, "", feature, brand, pos, rank, 0);
                    else
                        return Globals.GetSiteUrls().StaticizeSubCategory(cateif.CategoryId, cateif.RewriteName, (int)cateif.ParentCategoryId, cateif.ParentRewriteName, feature, brand, pos, rank, 0);
                }
            }
            else if (this.Page.Request.Path == "/BrandDetails.aspx")
            {
                int cid;
                int brand;
                int filter;
                int rank;

                //string model = "Brand_detail-{0}_cid-{1}_filter-{2}_rank-{3}.html";
                string model = "B{0}C{1}F{2}R{3}.html";

                string url;
                int.TryParse(this.Page.Request.QueryString["cid"], out cid);
                int.TryParse(this.Page.Request.QueryString["brandId"], out brand);
                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);

                url = string.Format(model, brand, cid, pos, rank);
                
                
                return url;

            }
            return "";
        }
    }
}
